package au.edu.uq.itee.engg2800.team19;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Observable;

import javax.swing.JPanel;


@SuppressWarnings("serial")
public class FrequencyPlot extends JPanel implements ActionListener,java.util.Observer{
private Graphics2D gRenderer;
//private double xInc;
final int OFFSET = 100;
final int Xdivisions=8;
final int Ydivisions=3;
final int TagLength=30;
private double Xinterval;
private double Yinterval;
private double fullbar;

private FrequenciesReceived myfreqs;

public FrequencyPlot(FrequenciesReceived freqs){
	this.setBackground(Color.gray);
	this.setPreferredSize(new Dimension(600,400));
	this.myfreqs=freqs;
	myfreqs.addObserver(this);
}
protected void paintComponent(Graphics g) {

    super.paintComponent(g);
    this.gRenderer = (Graphics2D)g;
    ArrayList<String> freqArray=Frequencies.enumsToStringArray();
    String[] percentMarks={"150","100","50","0"};
    gRenderer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
	int width = getWidth();
    int height = getHeight();

    //Draw the axis
    // Draw X Axis
    Line2D Xaxis=new Line2D.Double(OFFSET, (height-OFFSET), width-OFFSET, (height-OFFSET));
    gRenderer.draw(Xaxis);
    Xinterval= (Xaxis.getX2()-Xaxis.getX1())/Xdivisions;


    // Draw Y axis.
    Line2D Yaxis=new Line2D.Double(OFFSET, OFFSET, OFFSET, (height-OFFSET));
    gRenderer.draw(Yaxis);
    Yinterval = (Yaxis.getY2()-Yaxis.getY1())/Ydivisions;
    fullbar=(2*Yinterval);

    //draw division markers
    gRenderer.setPaint(Color.blue);
    for (int i=0;i<=Ydivisions;i++){
    	gRenderer.draw(new Line2D.Double(OFFSET-TagLength,OFFSET+(i*Yinterval),
    			OFFSET-1,OFFSET+(i*Yinterval)));
    	if (i<=Ydivisions){
    		g.drawString(percentMarks[i],(int) OFFSET-TagLength-21,(int) (OFFSET+(i*Yinterval)+5));
    	}
    }

    //X Axis
    String val=null;
    String direction="Recd";

    for (int i=0;i<=Xdivisions;i++){
		if ( i<Xdivisions){
			val=freqArray.get(i);
			double curr=myfreqs.getval(i+1);
			double percentage=(curr/100);
		    Rectangle2D myrect =new Rectangle2D.Double
	    	(1+OFFSET+(Xinterval*i),height-OFFSET- (fullbar*percentage),Xinterval-2,
	    			(fullbar*percentage));
		    gRenderer.setPaint(Color.green);
		    gRenderer.fill(myrect);
		}
			direction="Recd";

		gRenderer.setPaint(Color.blue);
    	gRenderer.draw(new Line2D.Double(OFFSET+(i*Xinterval), height-OFFSET+1,
    			OFFSET+(i*Xinterval),height-OFFSET+TagLength));

    	if (i<Xdivisions){
    		gRenderer.setPaint(Color.blue);
    		g.drawString(val,(int) (OFFSET+(i*Xinterval)+3),height-OFFSET+20);
    		g.drawString(direction,(int) (OFFSET+(i*Xinterval)+3),height-OFFSET+30);
    	}

    }
    //Label the axes
    g.drawString("Frequency (HZ)", (int) (OFFSET+ Xinterval*Xdivisions/2.25), height-OFFSET+52);
    g.drawString("(%)",  20, 200);
    g.drawString("Signal", 15, 210);
}
@Override
public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub

}
@Override
public void update(Observable arg0, Object arg1) {

		paintComponent(getGraphics());
		System.out.println("updated");
	}


}
